import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.geom.Circle;
import org.newdawn.slick.geom.Polygon;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.Color;

public class Play extends BasicGameState {

	private Shape circle;
	private Shape rectangle;
	private Shape polygon;
	private Image back;

	private float circlePosX = 30;
	private float circlePosY = 250;

	private boolean collides = false;

	public Play(int state) {
	}

	/**
	 * Creates a circle, image, rectangle, array of floats and a polygon.
	 */
	public void init(GameContainer gc, StateBasedGame sbg)
			throws SlickException {
		circle = new Circle(circlePosX, circlePosY, 15); // x-pos, y-pos, radius
		back = new Image("C:/Users/Dina/Documents/GameProject/pics/back.png");
		rectangle = new Rectangle(400, 250, 60, 130);

		float[] positions = new float[] { 100, 100, 40, 30, 20, 30, 200, 200,
				70, 300, 200, 350 };
		polygon = new Polygon(positions);
	}

	/**
	 * Makes method calls to draw the "back" button, circle and maze. Draws a
	 * string that shows if collision has been detected, for testing purposes
	 * only.
	 */
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
			throws SlickException {
		back.draw(690, 10);
		drawCircle(gc, sbg, g);
		drawMaze(gc, sbg, g);

		g.drawString("Collides: " + collides, 10, 30);
	}

	/**
	 * Updates the screen by performing method calls. Deals with back button on
	 * screen and any input. Checks if there is a collision between a polygon
	 * and circle.
	 */
	public void update(GameContainer gc, StateBasedGame sbg, int delta)
			throws SlickException {
		backButton(gc, sbg, delta);
		inputKeys(gc, sbg, delta);

		collides = polygon.intersects(circle);
		// collides = rectangle.intersects(circle);

	}

	/**
	 * Tracks mouse position and allows user to press the "back" button to enter
	 * another state.
	 */
	public void backButton(GameContainer gc, StateBasedGame sbg, int delta) {
		int posX = Mouse.getX();
		int posY = Mouse.getY();

		if ((posX > 690 && posX < 783) && (posY > 455 && posY < 490)) {
			if (Mouse.isButtonDown(0)) {
				sbg.enterState(0);
			}
		}
	}

	/**
	 * Manages the input from the keyboard. Checks if collides is true to stop
	 * the circle from moving any further in a particular direction.
	 */
	public void inputKeys(GameContainer gc, StateBasedGame sbg, int delta) {
		Input input = gc.getInput();

		if (input.isKeyDown(Input.KEY_DOWN)) {
			circlePosY += 0.1;
			if (collides) {
				circlePosY -= 2;
			}
		}
		if (input.isKeyDown(Input.KEY_UP)) {
			circlePosY -= 0.1;
			if (collides) {
				circlePosY += 2;
			}
		}
		if (input.isKeyDown(Input.KEY_LEFT)) {
			circlePosX -= 0.1;
			if (collides) {
				circlePosX += 2;
			}
		}
		if (input.isKeyDown(Input.KEY_RIGHT)) {
			circlePosX += 0.1;
			if (collides) {
				circlePosX -= 2;
			}
		}

	}

	/**
	 * Draws a circle and sets its position on the screen. Sets a colour and
	 * fills the circle with that colour.
	 */
	public void drawCircle(GameContainer gc, StateBasedGame sbg, Graphics g) {
		g.draw(circle);
		circle.setLocation(circlePosX, circlePosY);
		g.setColor(Color.blue);
		g.fill(circle);
	}
	
	/**
	 * Draws the maze on the screen.
	 */
	public void drawMaze(GameContainer gc, StateBasedGame sbg, Graphics g) {
		//TODO
		g.draw(rectangle);
		g.draw(polygon);
	}
	
	/**
	 * Returns the ID for this state.
	 */
	public int getID() {
		return 1;
	}
}